package View;

import Hander.LenderViewHander;
import Hander.LonginViewHander;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class LoginView extends JFrame {
    //一个标题
    JLabel nameLable=new JLabel("科研管理系统",JLabel.CENTER);
    SpringLayout springLayout=new SpringLayout();//弹簧布局
    //一个面板，面板总是填充完全
    JPanel centerPanel=new JPanel(springLayout);
    //六个组件
    JLabel userNameLabel=new JLabel("用户名:");
    JTextField userText=new JTextField();
    JLabel passwordLabel=new JLabel("密码:");
    JPasswordField passwordField=new JPasswordField();
    JButton loginBtn=new JButton("登录");
    JButton resetBtn=new JButton("重置");
    JButton zhuce=new JButton("注册");
    SystemTray systemTray;
    TrayIcon trayIcon;
    URL imgUrl = LoginView.class.getResource("/Image/R.png");

    LenderViewHander longinHander;
    //设置组件大小及样式
    public void fun(){
        int width=600;
        int height=400;
        setSize(width,height);//单位：像素px
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setVisible(true);
//        resetBtn.setVisible(true);
    }
    public void fun1(){
        setIconImage(new ImageIcon(imgUrl).getImage());
        //标题：nameLable
        nameLable.setFont(new Font("华文行楷",Font.PLAIN,40));
        Font centerFont=new Font("楷体",Font.PLAIN,20);
        //1、userNameLabel
        userNameLabel.setFont(centerFont);
        //2、lohinBtn
        loginBtn.setFont(centerFont);
        //3、resetBtn
        resetBtn.setFont(centerFont);
        //4、userText
        userText.setPreferredSize(new Dimension(200,20));
        //5、passwordLabel
        passwordLabel.setFont(centerFont);
        //6、passwordField
        passwordField.setPreferredSize(new Dimension(200,20));
        //7、zhuce
        zhuce.setFont(centerFont);//设置注册按钮样式
    }
    //组件组装
    public void fun2(){
        centerPanel.add(userNameLabel);
        centerPanel.add(userText);
        centerPanel.add(passwordLabel);
        centerPanel.add(passwordField);
        centerPanel.add(loginBtn);
        centerPanel.add(resetBtn);
        centerPanel.add(zhuce);//添加注册按钮

    }
    //组件布局
    public void fun3(){
        Spring userNameLabelwidth= Spring.width(userNameLabel);
        Spring userTextwidth=Spring.width(userText);
        Spring spacewidth = Spring.constant(20);
        Spring childwidth = Spring.sum(Spring.sum(userNameLabelwidth,userTextwidth),spacewidth);
        int offsetX=childwidth.getValue()/2;
        SpringLayout.Constraints userNameLabelC =springLayout.getConstraints(userNameLabel);
//        userNameLabelC.setX(Spring.constant(100));
        springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offsetX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
        userNameLabelC.setY(Spring.constant(50));
        //userText
        springLayout.putConstraint(SpringLayout.NORTH,userText,0,SpringLayout.NORTH,userNameLabel);
        springLayout.putConstraint(SpringLayout.WEST,userText,20,SpringLayout.EAST,userNameLabel);
        //passwordLabel
        springLayout.putConstraint(SpringLayout.EAST,passwordLabel,0,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,passwordLabel,20,SpringLayout.SOUTH,userNameLabel);
        //passwordField
        springLayout.putConstraint(SpringLayout.NORTH,passwordField,0,SpringLayout.NORTH,passwordLabel);
        springLayout.putConstraint(SpringLayout.WEST,passwordField,20,SpringLayout.EAST,passwordLabel);
        //loginBtn
        springLayout.putConstraint(SpringLayout.EAST,loginBtn,-30,SpringLayout.EAST,passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,loginBtn,20,SpringLayout.SOUTH,passwordLabel);
        //resetBtn
        springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,loginBtn);
        springLayout.putConstraint(SpringLayout.WEST,resetBtn,80,SpringLayout.EAST,loginBtn);
        //zhuce
        springLayout.putConstraint(SpringLayout.NORTH,zhuce,0,SpringLayout.NORTH,resetBtn);
        springLayout.putConstraint(SpringLayout.WEST,zhuce,80,SpringLayout.EAST,resetBtn);

    }
    //设置托盘
    public void fun4(){
        if(SystemTray.isSupported()){
            systemTray=SystemTray.getSystemTray();
            trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());
            //设置托盘图片自动缩放
            trayIcon.setImageAutoSize(true);
            try{
                systemTray.add(trayIcon);
            }catch (AWTException e){
                e.printStackTrace();
            }
            //最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowIconified(WindowEvent e) {
                    LoginView.this.dispose();

                }
            });
            //托盘监听事件
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clichCount=e.getClickCount();
                    if (clichCount==1){
                        LoginView.this.setExtendedState(JFrame.NORMAL);
                    }
                    LoginView.this.setVisible(true);
                }
            });
        }
    }


    public LoginView(){
        super("学生管理系统登录页面");
        LonginViewHander longinHander1=new LonginViewHander(this);
        Container contentPane=getContentPane();
        fun1();
        fun2();
        fun3();
        contentPane.add(nameLable,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
        loginBtn.addActionListener(longinHander1);//添加监听事件
        resetBtn.addActionListener(longinHander1);
        loginBtn.addKeyListener(longinHander1);
        zhuce.addActionListener(longinHander1);
        fun4();
        getRootPane().setDefaultButton(loginBtn);
        fun();
    }
    public static void main(String[] args) {
        new LoginView();
    }

    public JTextField getUserText() {
        return userText;
    }

    public JPasswordField getPasswordField() {
        return passwordField;
    }

}
